package stack.era.presentation.frame;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;

import stack.era.constants.FrameAttributes;
import stack.era.constants.ScreenAttributes;
import stack.era.constants.FrameAttributes.CardPanel;
import stack.era.presentation.hud.PlayerHUD;
import stack.era.presentation.main_menu.MainMenuPanel;
import stack.era.presentation.side_screen.SideScreenManager;
import stack.era.presentation.world.WorldPanel;

public class ERAFrame extends JFrame {

	private static final long serialVersionUID = -6620066803111115632L;
	private static ERAFrame instance = null;

	private ERAFramePopulateThread popThread;

	private CardLayout layout;
	private JPanel cardLayoutPanel;

	private PlayerHUD hud;

	private MainMenuPanel mainMenuPanel;
	private WorldPanel worldPanel;

	private ERAFrame() {
		super("ERA Discovery 1.0");

		ScreenAttributes.SCREEN_SIZE = Toolkit.getDefaultToolkit()
				.getScreenSize();
		this
				.setLocation(
						(int) (ScreenAttributes.SCREEN_SIZE.getWidth() / 2)
								- (int) (FrameAttributes.MAIN_FRAME_SIZE
										.getWidth() / 2),
						(int) (ScreenAttributes.SCREEN_SIZE.getHeight() / 2)
								- (int) (FrameAttributes.MAIN_FRAME_SIZE
										.getHeight() / 2));

		// Setup tooltips
		UIManager.put("ToolTip.foreground", new ColorUIResource(Color.BLACK));
		UIManager.put("ToolTip.background", new ColorUIResource(new Color(100,
				200, 200)));

		this.setSize(FrameAttributes.MAIN_FRAME_SIZE);
		this.setResizable(true);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		cardLayoutPanel = new JPanel();
		layout = new CardLayout();
		cardLayoutPanel.setLayout(layout);

		mainMenuPanel = new MainMenuPanel();
		cardLayoutPanel.add(mainMenuPanel, CardPanel.MAIN_MENU.getName());

		worldPanel = new WorldPanel();
		cardLayoutPanel.add(worldPanel, CardPanel.WORLD_PANEL.getName());

		this.setLayout(new BorderLayout());
		hud = new PlayerHUD();
		this.add(hud, BorderLayout.SOUTH);
		this.add(cardLayoutPanel, BorderLayout.CENTER);

		this.addWindowListener(new ERAFrameCloseControl());
	}

	public void openFrame() {
		this.setVisible(true);
		popThread = new ERAFramePopulateThread();
		popThread.start();
	}

	public void closeFrame() {
		this.setVisible(false);
		popThread.end();
		this.dispose();
	}

	public void showCard(CardPanel card) {
		if (card.equals(CardPanel.MAIN_MENU)) {
			hud.setVisible(false);
		} else {
			hud.setVisible(true);
		}
		layout.show(cardLayoutPanel, card.getName());
		this.setTitle(card.getName());
	}

	public synchronized static ERAFrame getInstance() {
		if (instance == null) {
			instance = new ERAFrame();
		}
		return instance;
	}

	public void populatePresentation() {
		SideScreenManager.getInstance().populateSideScreens();
		hud.populate();
		mainMenuPanel.populate();
		worldPanel.populate();
		ERAFrame.getInstance().repaint();
	}

}
